﻿using PLCS.Application.Contracts.EquipmentApp.Dtos;
using PLCS.Application.Contracts.PLCReadDataApp.Dtos;
using PLCS.Client.IService.ConfigManager;
using PLCS.Domain.Equipments;

using Volo.Abp.Application.Dtos;

namespace PLCS.Client.Service.ConfigManager;

public class PLCReadDataService : BaseService<PLCReadDataDto, PLCReadDataEditDto>, IPLCReadDataService
{
    public async Task CreatePLCReadDataListAsync(IEnumerable<PLCReadDataEditDto> plcReadDatas)
    {
        RequestParam requestParam = new(method: Method.Post, route: $"pLCReadData/list", parameter: plcReadDatas);
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task DeleteAllAsync()
    {
        RequestParam requestParam = new(method: Method.Delete, route: $"pLCReadData/all");
        var apiRes = await restClient.ExecuteAsync(requestParam);
        if (!apiRes.Status)
            throw new Exception(apiRes.Message);
    }

    public async Task<PagedResultDto<PLCReadDataDto>> GetListBySearchAsync(int skipCount, int maxResultCount, Guid? plcConfigNo, string? startBit, string? deviceNo)
    {
        var route = $"pLCReadData/bySearch?SkipCount={skipCount}&MaxResultCount={maxResultCount}";
        if (plcConfigNo != null && plcConfigNo != Guid.Empty)
            route += $"&plcConfigNo={plcConfigNo}";
        if (!string.IsNullOrEmpty(startBit))
            route += $"&startBit={startBit}";
        if (!string.IsNullOrEmpty(deviceNo))
            route += $"&deviceNo={deviceNo}";

        RequestParam requestParam = new(method: Method.Get, route: route);
        var apiRes = await restClient.ExecuteAsync<PagedResultDto<PLCReadDataDto>>(requestParam);
        if (apiRes.Status)
            return apiRes.Data;
        else
            return new PagedResultDto<PLCReadDataDto>();
    }
}